﻿/// <summary>
/// Copyright(c) 2010-2012 LZT CORPORATION, All Rights Reserved.
///
/// Workflow Designer Application MainPage
///
/// author   張強 
/// version  1.00   Date: 2010-11-02
/// </summary>

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Browser;
using System.Diagnostics;
using WorkflowDesigner.Workflow.Area;
using WorkflowDesigner.Workflow;
using System.Windows.Media;
using System.Windows.Input;
using WorkflowDesigner.Workflow.Item;

namespace WorkflowDesigner
{
    public partial class MainPage : UserControl
    {
        private ToolBox m_toolBox = null;
        private DesignView m_mainView = null;

        private WorkflowBase m_objDraging = null;
        private Point m_ptItemOffset;
        // 判断鼠标左键是否按下
        private bool m_isMouseDown = false;
        private Color m_clrBackground = Color.FromArgb(255, 199, 218, 240);

        public MainPage()
        {
            InitializeComponent();
            //MainPanel.Width = Utility.BrowserClientWidth;
            //MainPanel.Height = Utility.BrowserClientHeight - 135;
            MainPanel.Width = this.Width;
            MainPanel.Height = this.Height;
            MainPanel.Background = new SolidColorBrush(m_clrBackground);
            Application.Current.Host.Content.Resized += new EventHandler(IE_Resized);
        }

        void IE_Resized(object sender, EventArgs e)
        {
            // 浏览器大小变化
            double dWidth = Utility.BrowserClientWidth;
            double dHeight = Utility.BrowserClientHeight;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            Debug.WriteLine("MainPage OnMouseLeftButtonDown");
            this.m_isMouseDown = true;
            WorkflowBase item = m_toolBox.FindItemMouseOver();
            if (item != null)
            {
                item.IsMouseOver = false;
                this.m_isMouseDown = true;
                this.CaptureMouse();
                Point ptMainPage = e.GetPosition(MainPanel);
                m_ptItemOffset = e.GetPosition(item);
                switch (item.ItemType)
                {
                    case FlowItemType.Condition:
                        m_objDraging = new FlowCondition();
                        break;
                    case FlowItemType.End:
                        m_objDraging = new FlowEnd();
                        break;
                    case FlowItemType.Normal:
                        m_objDraging = new FlowNormal();
                        break;
                    case FlowItemType.Start:
                        m_objDraging = new FlowStart();
                        break;
                    default:
                        break;
                }
                if (m_objDraging != null)
                {
                    MainPanel.Children.Add(m_objDraging);
                    m_objDraging.MoveTo(
                        ptMainPage.X - m_ptItemOffset.X, ptMainPage.Y - m_ptItemOffset.Y);
                    m_objDraging.BringToTop();
                }
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            Debug.WriteLine("MainPage OnMouseLeftButtonUp");
            if (m_isMouseDown)
            {
                this.ReleaseMouseCapture();
                if (m_objDraging != null)
                {
                    Point ptMainView = e.GetPosition(m_mainView);
                    if ((ptMainView.X > 0.0) && (ptMainView.Y > 0.0))
                    {
                        ptMainView.X = ptMainView.X - m_ptItemOffset.X - 4.0;
                        ptMainView.Y = ptMainView.Y - m_ptItemOffset.Y - 4.0;
                        m_mainView.AddFlowItem(m_objDraging, ptMainView);
                    }
                    MainPanel.Children.Remove(m_objDraging);
                    m_objDraging = null;
                }
                this.m_isMouseDown = false;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (m_isMouseDown && (m_objDraging != null))
            {
                Point ptMainPage = e.GetPosition(MainPanel);
                m_objDraging.MoveTo(
                    ptMainPage.X - m_ptItemOffset.X, ptMainPage.Y - m_ptItemOffset.Y);
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Key == Key.Ctrl)
            {
                App.IsCtrlKeyDown = true;
            }
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            switch (e.Key)
            {
                case Key.Delete:
                    App.theMainView.DeleteSelectedFlowItem();
                    break;
                case Key.Ctrl:
                    App.IsCtrlKeyDown = false;
                    break;
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MainPanel.Width  = this.ActualWidth;
            MainPanel.Height = this.ActualHeight;
            // 工具栏重新定位
            if (m_toolBox == null)
            {
                m_toolBox = new ToolBox();
                MainPanel.Children.Add(m_toolBox);
            }
            m_toolBox.SetValue(Canvas.TopProperty, 5.0);
            m_toolBox.SetValue(Canvas.LeftProperty, 5.0);
            m_toolBox.Width = 200.0;
            m_toolBox.Height = MainPanel.Height - 10.0;
            // 设计窗口重新定位
            if (m_mainView == null)
            {
                m_mainView = new DesignView();
                MainPanel.Children.Add(m_mainView);
                App.theMainView = m_mainView;
            }
            m_mainView.SetValue(Canvas.TopProperty, 5.0);
            m_mainView.SetValue(Canvas.LeftProperty, 210.0);
            m_mainView.Width = MainPanel.Width - 215.0;
            m_mainView.Height = MainPanel.Height - 10.0;
            // SizeChanged 时候 GridLine 需要重新绘制
            // 传递IsGridLineShow给View后，View会自己判断是否需要绘制以及如何调整
            m_mainView.ShowGridLine(m_mainView.IsGridLineShow);
            m_mainView.TryResize();
        }
    }
}
